import 'dart:ffi';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:jl_plugin/bean/BlueDevice.dart';

import 'bean/EqInfo.dart';
import 'bean/JlResult.dart';
import 'bean/KeySettings.dart';
import 'jl_plugin_platform_interface.dart';

/// An implementation of [JlPluginPlatform] that uses method channels.
class MethodChannelJlPlugin extends JlPluginPlatform {
  /// The method channel used to interact with the native platform.


  @override
  Future<bool?> initSdk(Map<String,dynamic> paramMap) async{
    final result = await methodChannel.invokeMethod<bool>('initSdk',paramMap) ;
    return result;
  }

  @override
  Future<bool?> startScanBlue() async{
    final result = await methodChannel.invokeMethod<bool>('startScanBlue') ;
    return result;
  }

  @override
  Future<bool?> stopScanBlue() async{
    final result = await methodChannel.invokeMethod<bool>('stopScanBlue') ;
    return result;
  }

  @override
  Future<Map?> connectBlue(String address,int connectWay) async{
    final result = await methodChannel.invokeMethod<Map?>('connectBlue',{"address":address,"connectWay":connectWay}) ;
    return result;
  }

  @override
  Future<bool?> disconnectBlue(BlueDevice device) async{
    final result = await methodChannel.invokeMethod<bool?>('disconnectBlue',device.toJson()) ;
    return result;
  }

  @override
  Future<bool?> disconnectBlue2(String address) async{
    final result = await methodChannel.invokeMethod<bool?>('disconnectBlue',{"address" : address}) ;
    return result;
  }

  @override
  Future<JlResult> configKeySettings(List<KeySetting> keySettingList) async{
    final resultMap = await methodChannel.invokeMethod<Map?>('configKeySettings',{
      "keySettingList":keySettingList.map((e) => e.toJson()).toList()
    });
   var result= JlResult.fromJson(Map.from(resultMap ?? {}));
   return result;
  }

  @override
  Future<Map> getDevicesSetting(int mask) async{
    final result = await methodChannel.invokeMethod<Map?>('getDevicesSetting',{"mask":mask}) ;
    return result!;
  }

 @override
  Future<bool?> settingEq(EqInfo eqInfo) async{
    print("ssssss:${eqInfo.toJson()}");
   return await methodChannel.invokeMethod<bool?>('eqSetting',eqInfo.toJson()) ;
  }

  @override
  Future getEqSetting() async{
     methodChannel.invokeMethod('getEQSetting') ;
  }

  @override
  Future sendCmd(String cmd) async{
    methodChannel.invokeMethod('sendCustomCmd',{"customData":cmd}) ;
  }

  @override
  Future<Map?> modifyHeadsetFunctions(int attrType, List<Int> paramData) async{
    return await methodChannel.invokeMethod<Map>('modifyHeadsetFunctions',{"type":attrType,"paramData":paramData}) ;
  }

  @override
  Future<Map?> changeDeviceName(String name) async{
    return await methodChannel.invokeMethod<Map>('changeDeviceName',{"name":name}) ;
  }

  @override
  Future<Map?> rebootDevice() async{
    return await methodChannel.invokeMethod<Map>('rebootDevice') ;
  }

  @override
  Future<Map?> getCurrentDevices() async{
    return await methodChannel.invokeMethod<Map>('getCurrentDevices') ;
  }

  @override
  Future<List?> getHistoryBluetoothDeviceList() async{
    var result= await methodChannel.invokeMethod<List?>('getHistoryBluetoothDeviceList') ;
    return result;
  }

  @override
  Future<Map?> findHistoryBluetoothDevice(String address) async{
    var result= await methodChannel.invokeMethod<Map?>('findHistoryBluetoothDevice',{"address":address}) ;
    return result;
  }

  @override
  Future<bool?> isConnecting() async{
    return await methodChannel.invokeMethod<bool?>('isConnecting') ;
  }

  @override
  Future<bool?> isConnectioned() async{
    return await methodChannel.invokeMethod<bool?>('isConnectioned') ;
  }

  @override
  Future<bool?> isScanning() async{
    return await methodChannel.invokeMethod<bool?>('isScanning') ;
  }

  @override
  Future<bool?> isInit() async{
    return await methodChannel.invokeMethod<bool?>('isInit') ;
  }

  @override
  Future<bool?> removeHistoryBluetoothDevice(String address) async{
    return await methodChannel.invokeMethod<bool?>('removeHistoryBluetoothDevice',{"address":address}) ;
  }

  @override
  Future<bool?> stopReconnect() async{
    return await methodChannel.invokeMethod<bool?>('stopReconnect') ;
  }

  @override
  Future<bool?> startReconnect() async{
    return await methodChannel.invokeMethod<bool?>('startReconnect') ;
  }

  @override
  Future<bool?> startRecord() async{
    return await methodChannel.invokeMethod<bool?>('startRecord') ;
  }

  @override
  Future<bool?> stopRecord() async{
    return await methodChannel.invokeMethod<bool?>('stopRecord') ;
  }

  // @override
  // Future<bool?> getRecordStatus() async{
  //   return await methodChannel.invokeMethod<bool?>('getRecordStatus') ;
  // }



  @override
  Future<bool?> startDecode() async{
    return await methodChannel.invokeMethod('startDecode') ;
  }

  Future<bool?> stopDecode() async{
    return await methodChannel.invokeMethod('stopDecode') ;
  }

  Future<bool?> releaseDecode() async{
    return await methodChannel.invokeMethod('releaseDecode') ;
  }

  Future decodeOpus(Uint8List data) async{
    return await methodChannel.invokeMethod('decodeOpus',{"data":data}) ;
  }
}
